package com.suyuan.controller.admin;

import com.github.pagehelper.PageInfo;
import com.suyuan.entity.Station;
import com.suyuan.entity.TransportRouteInfor;
import com.suyuan.service.TransportRouteInforService;
import com.suyuan.utils.JsonMessage;
import com.suyuan.web.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 *  线路管理控制器
 *
 */
@Controller
@RequestMapping(value = "/admin/transportRouteInfor")
public class TransportRouteInforController {

    @Resource(name = "transportRouteInforService")
    private TransportRouteInforService transportRouteInforService;

    /**
     * 跳转添加页面
     * @return
     */
    @RequestMapping( value = "add")
    public String add(){
        return "route_add";
    }

    /**
     * 跳完编辑页面
     * @return
     */
    @RequestMapping(value = "/edit")
    public String edit(Integer id,ModelMap model){
        TransportRouteInfor route = transportRouteInforService.findById(id);
        model.addAttribute("route",route);
        return "route_edit";
    }

    /**
     * 编辑完后更新操作
     * @return
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(TransportRouteInfor route){
        int code = transportRouteInforService.update(route);
        if(code == 1){
            return JsonMessage.success("更新成功");
        }
        return JsonMessage.error("更新失败");
    }

    /**
     * 保存
     * @param route
     * @return
     */
    @RequestMapping(value = "save" , method = RequestMethod.POST)
    @ResponseBody
    public Object save(TransportRouteInfor route, @RequestParam String[] stationNames){

        System.out.println("route:"+route);
        for(String s : stationNames){
            System.out.println(s);
        }
        try {
            transportRouteInforService.addRouteAndStation(route,stationNames);
            return JsonMessage.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonMessage.error("添加失败");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping( value = "delete")
    @ResponseBody
    public Object delete(Integer id){
        int code = transportRouteInforService.delete(id);
        if(code == 1){
            return JsonMessage.success("删除成功");
        }
        return JsonMessage.error("删除失败");
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list")
    public String list(Integer routeId, Integer stationNo, Page page, ModelMap model){
        PageInfo info = transportRouteInforService.inquiry(routeId,stationNo,page);
        model.addAttribute("page",info);
        model.addAttribute("routeId",routeId);
        model.addAttribute("stationNo",stationNo);
        return "route_list";
    }

    /**
     * 批量删除
     */
    @RequestMapping(value = "/deleteByIds")
    @ResponseBody
    public Object deleteByIds(String ids){
        if(StringUtils.isBlank(ids)){
            return null;
        }
        String[] idArr = null;
        if(ids.length() >= 2){
            idArr = ids.split(",");
        }else{
            idArr = new String[]{ids};
        }
        for(String id : idArr){
            transportRouteInforService.delete(Integer.parseInt(id));
        }
        return null;
    }
}
